﻿package  {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Element extends MovieClip{
		private var currentX:uint;
		private var currentY:uint;
		
		private var index:uint;//1-4
		
		private var otherElement:Element;//交换用 对方元素
		private var otherIndex:uint;//对方的图标
		private var frameCount:uint;
		
		private var dropTimes:uint;
		
		private var moveFlag:String;//告知移动方向
		private var readyFlag:Boolean;//检测以该元素为中心待删除元素过程是否结束 true结束，false未结束
		private var plusCount:uint;//加分依据
		//private var removeElemArr:Array;//用于放置以本元素为中心的有待消除的元素
		
		public function Element(currentX, currentY, index:uint) {
			//this.scaleX = 1.2;
			//this.scaleY = 1.2;
			
			this.currentX = currentX;
			this.currentY = currentY;
			
			//图标样式
			this.index = index;
			this.mc.gotoAndStop(index);
			
			this.readyFlag = true;
			this.plusCount = 0;
			//this.removeElemArr = new Array();
			
			this.addEventListener(MouseEvent.CLICK, clickHandler);
			
			this.addEventListener(GameEvent.ElementMove, elementMoveHandler);//单个元素交换
			this.addEventListener(GameEvent.ElementDrop, elementDropHandler);//元素掉落
			this.addEventListener(GameEvent.ElementMoveBack, elementMoveBackHandler);
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
		}
		
		private function removeHandler(e:Event):void{
			this.removeEventListener(MouseEvent.CLICK, clickHandler);
			
			this.removeEventListener(GameEvent.ElementMove, elementMoveHandler);//单个元素交换
			this.removeEventListener(GameEvent.ElementDrop, elementDropHandler);//元素掉落
			this.addEventListener(GameEvent.ElementMoveBack, elementMoveBackHandler);
			
			this.removeEventListener(Event.ENTER_FRAME, walkAnimationHandler);
			this.removeEventListener(Event.ENTER_FRAME, dropAnimationHandler);
			this.removeEventListener(Event.ENTER_FRAME, walkBackAnimationHandler);
			
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
		}
		
		//设置图标
		private function setIndex(i:uint):void{
			this.index = i;
			this.mc.gotoAndStop(i);
		}
		
		public function getIndex():uint{
			return this.index;
		}
		
		public function getCurrentX():uint{
			return this.currentX;
		}
		
		public function getCurrentY():uint{
			return this.currentY;
		}
		
		public function setCurrentY(i:uint):void{
			this.currentY = i;
		}
		
		public function getReadyFlag():Boolean{
			return this.readyFlag;
		}
		
		public function setReadyFlag(f:Boolean):void{
			this.readyFlag = f;
		}
		
		public function getPlusCount():uint{
			return this.plusCount;
		}
		
		public function setPlusCount(c:uint):void{
			this.plusCount = c;
		}
		
		public function getMoveFlag():String{
			return this.moveFlag;
		}
		
		public function setMoveFlag(str:String):void{
			this.moveFlag = str;
		}
		
		/*public function setRemoveElemArr(arr:Array):void{
			this.removeElemArr = arr;
		}
		
		public function getRemoveElemArr():Array{
			return this.removeElemArr;
		}
		
		public function pushRemveElem(e:Element){
			this.removeElemArr.push(e);
		}*/
		
		//点击响应
		private function clickHandler(e:MouseEvent):void{
			if(MainPanel.mouseEnableFlag == false || MainPanel.monkeyWalkFlag == true)return;//当鼠标锁住时，不响应操作
			
			if(MainPanel.selectedFlag){
				MainPanel.selectedFlag = false;
				
				this.gotoAndStop("stay");
				
				var otherE:Element = MainPanel.mixArr[0];
				if(otherE == this)	
					MainPanel.mixArr = new Array();	//自身取消选中
				else{
					var ax = otherE.getCurrentX();
					var ay = otherE.getCurrentY();
					var bx = this.currentX;
					var by = this.currentY;
					
					if(Math.abs(ax - bx) == 1 && ay == by || ax == bx && Math.abs(ay - by) == 1){
						MainPanel.mixArr.push(this);//非自身，加入对换列表
						//双双触发对换事件
						MainPanel.mixArr[0].gotoAndStop("stay");
						this.dispatchEvent(new GameEvent(GameEvent.ElementMove));
					}
					else{//非邻居元素
						MainPanel.mixArr[0].gotoAndStop("stay");
						MainPanel.selectedFlag = true;
						MainPanel.mixArr = new Array();	//自身取消选中
						MainPanel.mixArr.push(this);//将本元素添加到对换列表首位
						
						this.gotoAndPlay("selected");//播放点击动画
					}
				}
			}
			else{//初次选择
				MainPanel.selectedFlag = true;
				MainPanel.mixArr = new Array();
				MainPanel.mixArr.push(this);//将本元素添加到对换列表首位
				
				this.gotoAndPlay("selected");//播放点击动画
			}
		}
		
		//对换事件
		private function elementMoveHandler(e:GameEvent):void{
			//对方元素
			this.otherElement = MainPanel.mixArr[0];
			this.otherIndex = otherElement.getIndex();
			this.frameCount = 0;
			MainPanel.mouseEnableFlag = false;
			
			//this.readyFlag = false;
			//this.otherElement.setReadyFlag(false);
			
			if(this.frameCount == 0){
				//根据坐标判断前进方向
				if(otherElement.getCurrentX() == this.currentX){
					if(otherElement.getCurrentY() < this.currentY){
						//up
						this.setMoveFlag("up");
						this.gotoAndPlay("toUp");
						otherElement.setMoveFlag("down");
						otherElement.gotoAndPlay("toDown");
					}
					else{
						//down
						this.setMoveFlag("down");
						this.gotoAndPlay("toDown");
						otherElement.setMoveFlag("up");
						otherElement.gotoAndPlay("toUp");
					}
				}
				else{
					if(otherElement.getCurrentX() < this.currentX){
						//left
						this.setMoveFlag("left");
						this.gotoAndPlay("toLeft");
						otherElement.setMoveFlag("right");
						otherElement.gotoAndPlay("toRight");
					}
					else{
						//ahead
						this.setMoveFlag("right");
						this.gotoAndPlay("toRight");
						otherElement.setMoveFlag("left");
						otherElement.gotoAndPlay("toLeft");
					}
				}
			}
			
			this.addEventListener(Event.ENTER_FRAME, walkAnimationHandler);//移动动画
		}
		
		//移动动画
		private function walkAnimationHandler(e:Event):void{
			if(this.frameCount == 4){//只移动一次，单程
				this.gotoAndStop("stay");
				otherElement.gotoAndStop("stay");
				
				var tmpInde:uint = this.index;
				this.setIndex(otherIndex);//对换
				otherElement.setIndex(tmpInde);
				
				//触发检索消除项事件
				if(MainPanel.mixArr.length > 0){
					this.parent.parent.parent.dispatchEvent(new GameEvent(GameEvent.CheckRemove));
				}
				this.removeEventListener(Event.ENTER_FRAME, walkAnimationHandler);
				return;
			}
			
			frameCount ++;
		}
		
		//元素掉落动画
		private function elementDropHandler(e:GameEvent):void{
			var tmpE:Array = e.params as Array;
			
			dropTimes = tmpE[0];
			frameCount = 0;
			this.setReadyFlag(false);
			this.gotoAndPlay("toDown");
			this.addEventListener(Event.ENTER_FRAME, dropAnimationHandler);
		}
		
		private function dropAnimationHandler(e:Event):void{
			if(frameCount == 4){
				if(dropTimes < 2){
					this.y = (this.currentY + 0.5) * MainPanel.cellWidth;
					this.gotoAndStop("stay");
					this.setReadyFlag(true);
					
					this.removeEventListener(Event.ENTER_FRAME, dropAnimationHandler);
					return;
				}
				else{
					this.y += MainPanel.cellWidth;
					this.gotoAndPlay("toDown");
					frameCount = 0;
					
					dropTimes --;
				}
			}
			frameCount ++;
		}
		
		//对换返回事件
		private function elementMoveBackHandler(e:GameEvent):void{
			//对方元素
			this.otherElement = MainPanel.mixArr[0];
			this.otherIndex = otherElement.getIndex();
			this.frameCount = 0;
			MainPanel.mouseEnableFlag = false;
			
			if(this.getMoveFlag() == "up"){
				this.gotoAndPlay("toUp");
				otherElement.gotoAndPlay("toDown");
			}
			else if(this.getMoveFlag() == "down"){
				this.gotoAndPlay("toDown");
				otherElement.gotoAndPlay("toUp");
			}
			else if(this.getMoveFlag() == "left"){
				this.gotoAndPlay("toLeft");
				otherElement.gotoAndPlay("toRight");
			}
			else if(this.getMoveFlag() == "right"){
				this.gotoAndPlay("toRight");
				otherElement.gotoAndPlay("toLeft");
			}
			
			this.addEventListener(Event.ENTER_FRAME, walkBackAnimationHandler);//移动动画
		}
		
		//移动动画
		private function walkBackAnimationHandler(e:Event):void{
			if(this.frameCount == 4){//只移动一次，单程
				this.gotoAndStop("stay");
				otherElement.gotoAndStop("stay");
				
				var tmpInde:uint = this.index;
				this.setIndex(otherIndex);//对换
				otherElement.setIndex(tmpInde);
				
				MainPanel.mixArr = new Array();
				MainPanel.mouseEnableFlag = true;
				
				this.removeEventListener(Event.ENTER_FRAME, walkBackAnimationHandler);
				return;
			}
			
			frameCount ++;
		}
	}
	
}
